package com.hdy.orderforthree.mvp;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hdy.net.order.bean.DishBean;
import com.hdy.orderforthree.R2;
import com.hdy.orderforthree.adapter.BaseAdapter;
import com.hdy.orderforthree.adapter.BillAdapter;
import com.hdy.orderforthree.pos.ConcretePosFactory;
import com.hdy.orderforthree.pos.DGJPos;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BillView extends BaseView implements IBillContract.IView{

    @BindView(R2.id.bill_recyclerView)
    RecyclerView mBillRecyclerView;
    @BindView(R2.id.bill_pre_money)
    TextView mPreMoney;
    @BindView(R2.id.bill_money)
    TextView mMoney;

    BillAdapter mBillAdapter;
    List<DishBean> mBillDetail;

    private TitleBarPresenter.UpdateViewCallback mUpdateViewCallbackCallback;

    public BillView(View view) {
        super(view);
        mBillDetail = new ArrayList<>();

        LinearLayoutManager layoutManager = new LinearLayoutManager(mView.getContext());
        mBillRecyclerView.setLayoutManager(layoutManager);
        mBillAdapter = new BillAdapter(mBillDetail);
        mBillRecyclerView.setAdapter(mBillAdapter);
        mBillAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<DishBean>() {
            @Override
            public void OnItemClick(View view, BaseAdapter.ViewHolder holder, int position) {

            }

            @Override
            public void OnPlusClick(View view, BaseAdapter.ViewHolder holder, int position, DishBean dish) {
                ConcretePosFactory.getInstance().getPos().addCart(dish);
                updateBillView(position);
            }

            @Override
            public void OnMinusClick(View view, BaseAdapter.ViewHolder holder, int position, DishBean dish) {
                ConcretePosFactory.getInstance().getPos().removeCart(dish);
                updateBillView(position);
            }

            @Override
            public void onDeleteClick(View view, BaseAdapter.ViewHolder holder, int position, DishBean dishBean) {
                ConcretePosFactory.getInstance().getPos().removeCartByDish(dishBean);
                updateBillView(-1);
            }

            private void updateBillView(int position) {
                if (mUpdateViewCallbackCallback != null) {
                    mUpdateViewCallbackCallback.showTotalOrderNumberCorner();
                }
                if (position >= 0) {
                    mBillAdapter.notifyItemChanged(position);
                } else {
                    mBillAdapter.notifyDataSetChanged();
                }

                showMoney();
            }
        });
    }

    public void setUpdateViewCallback(TitleBarPresenter.UpdateViewCallback cb) {
        mUpdateViewCallbackCallback = cb;
    }

    @Override
    public void showBill() {
        mBillDetail.clear();
        if (ConcretePosFactory.getInstance().getPos() instanceof DGJPos) {
            DGJPos pos = (DGJPos) ConcretePosFactory.getInstance().getPos();
            if (pos.getConfirmOrderRequest() != null) {
                mBillDetail.addAll(pos.getConfirmOrderRequest().getDishes());
            }
        }
        mBillAdapter.notifyDataSetChanged();
    }

    @Override
    public void showMoney() {
        if (mBillAdapter != null) {
            float[] money = mBillAdapter.getMoney();
            mPreMoney.setText(String.valueOf(money[0]));
            mMoney.setText(String.valueOf(money[1]));
        }
    }

    public void notifyDataSetChanged() {
        if (mBillAdapter != null) {
            mBillAdapter.notifyDataSetChanged();
        }
    }
}
